package chapter8.poly_.ployarr;

//多态的应用--多态数组
//数组的定义类型为父类类型，里面保存的实际元素为子类类型
public class PloyArray {
    public static void main(String[] args) {
        //创建一个数组，存放一个Person对象，2个Studen对象，和两个Teacher对象
        Person[] persons = new Person[5];
        persons[0] = new Person("jack", 20);
        persons[1] = new Student("tx", 20, 80.0);
        persons[2] = new Student("tt", 21, 88.0);
        persons[3] = new Teacher("xx", 20, 8000.0);
        persons[4] = new Teacher("xt", 20, 60000.0);
        for (int i = 0; i < persons.length; i++) {
            //动态绑定机制
            //运行时根据运行类型调用相应的方法
            persons[i].say();
            if (persons[i] instanceof Student){
            Student stu = (Student)persons[i];
            stu.Study();
            }
            else if(persons[i] instanceof Teacher){
                Teacher tea = (Teacher)persons[i];
                tea.teach();
            }
            else if(persons[i] instanceof Person){

            }
            else{
                System.out.println("类型有误");
            }


        }
        //向下转型
    }
}
